package com.xneure.enging.ui;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.xneure.enging.event.Notify;
import com.xneure.enging.event.Notify.NotifyType;
import com.xneure.enging.ui.event.UiEvent;
import com.xneure.utils.bean.GUID;

public abstract class UiElement{
	private transient List<UiElement> children;
	private GUID id = GUID.randomID();
	private transient Map<String,Object> style;
	private transient Map<String,Object> property;
	private Collection<UiEvent> event;
	private String type;
	private transient Notify notify;
	private GUID parentId;

	public UiElement(UiElement par){
		if(null != par){
			parentId = par.getId();
		}
		type = this.getClass().getSimpleName().substring(2);
		notify(NotifyType.CREATE,"CREATE",this);
	}

	public void addElement(UiElement ele){
		if(null == children){
			children = new ArrayList<UiElement>();
		}
		children.add(ele);
	}

	public UiElement getElement(GUID id){
		for(UiElement e:children){
			if(e.id.equals(id)){
				return e;
			}
		}
		return null;
	}

	public List<UiElement> getChildren(){
		return children;
	}

	public Object getStyle(String name){
		for(Map.Entry<String,Object> pre:style.entrySet()){
			if(pre.getKey().equals(name)){
				return pre.getValue();
			}
		}
		return null;
	}

	public GUID getParentId(){
		return parentId;
	}

	public Map<String,Object> getStyle(){
		return style;
	}

	public GUID getId(){
		return id;
	}

	public void setId(GUID id){
		this.id = id;
		if(null != notify){
			notify.setSrcId(id);
		}
	}

	public void setStyle(String name,Object value){
		if(null == style){
			style = new HashMap<String,Object>();
		}
		style.put(name,value);
		notify(NotifyType.CSS,name,value);
	}

	public Collection<UiEvent> getEvent(){
		return event;
	}

	public void addEvent(UiEvent event){
		if(null == this.event){
			this.event = new ArrayList<UiEvent>();
		}
		this.event.add(event);
	}

	public String getType(){
		return type;
	}

	public void setType(String type){
		this.type = type;
	}

	public Map<String,Object> getProperty(){
		return property;
	}

	@SuppressWarnings("unchecked")
	public <O> O getProperty(String name){
		return (O)property.get(name);
	}

	public void setProperty(Map<String,Object> property){
		this.property = property;
	}

	public void setProperty(String name,Object pre){
		if(null == this.property){
			this.property = new HashMap<String,Object>();
		}
		this.property.put(name,pre);
		notify(NotifyType.ATTRIBUTE,name,pre);
	}

	protected void notify(NotifyType type,String name,Object val){
		if(isNotify()){
			if(null == notify){
				notify = new Notify(id);
			}
			notify.change(type,name,val);
		}
	}

	public void remove(UiElement ele){
		children.remove(ele);
		notify(NotifyType.DEL,"CHILDREN",ele.getId());
	}

	public boolean isNotify(){
		return true;
	}
}
